package com.cvs.controller;

import com.cvs.bean.*;
import com.cvs.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
   @Autowired
    private UserService userService;
   @Autowired
   private LevelService levelService;
   @Autowired
  private   SignService signService;
   @Autowired
   private JiFenService jiFenService;
@Autowired
private ActivityService activityService;
    @RequestMapping("/home")
    public String home(int uid, Model model){
        List<Level> levels=levelService.findAll();
        System.out.println(levelService.findAll());
        String levelname="";
      User user= userService.findOne(uid);
        for (Level le: levels) {
            if(user.getJinyan()>=le.getMoney()){
         levelname =le.getName();
            }
        }
        model.addAttribute("levelname",levelname);
      model.addAttribute("user",user);
        model.addAttribute("levels",levels);
        return "home";
    }
    @RequestMapping("/sign")
    public String sign(int uid, Model model){//跳转页面
        model.addAttribute("uid",uid);
        List<Activity> activities =activityService.findAll();
        model.addAttribute("activityies",activities);
        return "sign";
    }
    @PostMapping("/actlog")
    public @ResponseBody Map actlog(int uid) {//提交签到
        Map map=new HashMap();
        map.put("status",200);
        map.put("msg","查询成功");
        map.put("data",jiFenService.findCount(uid));
        return map;
    }
    @PostMapping("/sign")
    public @ResponseBody String sign1(int uid, Model model){//提交签到
        //Date
        Date date =new Date();
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("dd");//yyyy-mm-dd HH:MM:SS
       String day= simpleDateFormat.format(date);
       System.out.println(day);
        Sign sign =new Sign();
        sign.setDay(Integer.parseInt(day));
        sign.setCreatetime(date);
        sign.setUid(uid);
      int count=  signService.addSign(sign);
      System.out.println(count);
        return count+"";
    }
    @PostMapping("/dayList")
    public @ResponseBody Map dayList(int uid){//提交签到
        //Date
        //Date date =new Date();
        //SimpleDateFormat simpleDateFormat =new SimpleDateFormat("mm");//yyyy-mm-dd HH:MM:SS
       // String mm= simpleDateFormat.format(date);
    Map map=new HashMap();
   map.put("status",200);
   map.put("msg","查询成功");
   map.put("data",signService.signDay(uid));
        return map;
    }
    @RequestMapping("/dayCount")
    public @ResponseBody Map dayCount(int uid) {

        Map map = new HashMap();
        map.put("status", 200);
        map.put("msg", "查询成功");
        map.put("data", signService.signCount(uid));
        return map;
    }
    @PostMapping("/lqjifen")
    public @ResponseBody Map lqjifen(int uid,int actid) {
        //根据actid根据id获取活动具体内容
        //需要查询活动详情信息
        Activity activity =activityService.findOne(actid);
        User user= userService.findOne(uid);
      user.setJifen(user.getJifen()+activity.getMoney());
     int flag =userService.updateUser(user);
     //增加积分记录
        JiFen jiFen =new JiFen();
        jiFen.setActivity(activity);
        jiFen.setMoney(activity.getMoney());
        jiFen.setUid(uid);
        jiFen.setCreatetime( new Date());
      jiFenService.addJiFenLog(jiFen);

        Map map = new HashMap();
      if(flag==1){
          map.put("status", 200);
          map.put("msg", "领取成功");
          map.put("data", null);
      }else {
          map.put("status", 0);
          map.put("msg", "领取失败");
          map.put("data", null);
      }
        return map;
    }
    @RequestMapping("/jifenlog")
    public String jifenlog(int uid, Model model){//跳转页面
        User user= userService.findOne(uid);//查询用户原始积分数量
        model.addAttribute("user",user);
        model.addAttribute("list", jiFenService.findAll(uid));

        return "jifenlog";
    }
}
